home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Pascal Super Library
/
Pascal Super Library (CW International)(1997).bin
/
DELPHI32
/
SEARCH
/
RUBICON
/
RUBICON.ZIP
/
PREVINST.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1996-10-22
|
2KB
|
106 lines
{$B-,G+,X+} {* Boolean evaluation, Generate 286 code, eXtended syntax *}
unit PrevInst;
interface
uses
{$IFDEF WIN32}
Windows,
{$ELSE}
WinTypes, WinProcs,
{$ENDIF}
SysUtils;
{$IFDEF WIN32}
function DoIExist(WndTitle : String) : Boolean;
implementation
function DoIExist(WndTitle : String) : Boolean;
var
hSem : THandle;
hWndMe,
hWndPrev : HWnd;
semNm,
wTtl : Array[0..256] of Char;
begin
Result := False;
//Initialize arrays
StrPCopy(semNm, 'SemaphoreName');
StrPCopy(wTtl, WndTitle);
//Create a Semaphore in memory - If this is the first instance, then
//it should be 0.
hSem := CreateSemaphore(nil, 0, 1, semNm);
//Now, check to see if the semaphore exists
if ((hSem <> 0) AND (GetLastError() = ERROR_ALREADY_EXISTS)) then begin
CloseHandle(hSem);
//We'll first get the currently executing window's handle then change its title
//so we can look for the other instance
hWndMe := FindWindow(nil, wTtl);
SetWindowText(hWndMe, 'zzzzzzz');
//What we want to do now is search for the other instance of this window
//then bring it to the top of the Z-order stack.
hWndMe := FindWindow(nil, wTtl);
if (hWndMe <> 0) then begin
if IsIconic(hWndMe) then
ShowWindow(hWndMe, SW_SHOWNORMAL)
else
SetForegroundWindow(hWndMe);
end;
Result := True;
//Could put the Halt here, instead of in the FormCreate method,
//unless you want to do some extra processing.
//Halt;
end;
end;
{$ELSE}
type
PHWND = ^HWND;
function EnumFunc(Wnd:HWND; TargetWindow:PHWND): bool; export;
procedure GotoPreviousInstance;
implementation
function EnumFunc(Wnd:HWND; TargetWindow:PHWND): bool;
var
ClassName : array[0..30] of char;
begin
Result := true;
if GetWindowWord(Wnd,GWW_HINSTANCE) = hPrevInst then
begin
GetClassName(Wnd,ClassName,30);
if StrIComp(ClassName,'TApplication') = 0 then
begin
TargetWindow^ := Wnd;
Result := false;
end;
end;
end;
procedure GotoPreviousInstance;
var
PrevInstWnd : HWND;
begin
PrevInstWnd := 0;
EnumWindows(@EnumFunc,longint(@PrevInstWnd));
if PrevInstWnd <> 0 then
if IsIconic(PrevInstWnd) then
ShowWindow(PrevInstWnd,SW_RESTORE)
else
BringWindowToTop(PrevInstWnd);
end;
{$ENDIF}
end.